// 测试MCP工具自动注册到system prompt功能
const { ToolManager } = require('../dist/tools/index.js');

async function testMCPAutoRegister() {
    const toolManager = new ToolManager();
    
    console.log('=== 🎯 MCP工具自动注册测试 ===\n');
    
    const serverName = 'auto-test-server';
    const mcpServerPath = 'E:\\mcp_server\\mcp_server\\out';
    
    console.log('📋 步骤1: 查看连接前的工具状态');
    console.log(`本地工具数量: ${toolManager.getToolCount()}`);
    console.log(`包含MCP远程工具的总数量: ${toolManager.getAllToolsIncludingMCP().length}`);
    console.log('');
    
    try {
        console.log('🔗 步骤2: 连接MCP服务器（应自动注册远程工具）');
        const connectResult = await toolManager.executeTool('mcp-client', {
            action: 'connect',
            serverName: serverName,
            command: 'node index.js',
            cwd: mcpServerPath
        });
        
        console.log('连接结果:');
        console.log(connectResult.substring(0, 500) + '...');
        console.log('');
        
        console.log('📊 步骤3: 查看连接后的工具状态');
        console.log(`本地工具数量: ${toolManager.getToolCount()}`);
        console.log(`包含MCP远程工具的总数量: ${toolManager.getAllToolsIncludingMCP().length}`);
        console.log('');
        
        console.log('🔧 步骤4: 列出远程工具');
        const allTools = toolManager.getAllToolsIncludingMCP();
        const remoteTools = allTools.filter(tool => tool.isMCPRemote);
        console.log(`发现 ${remoteTools.length} 个远程工具:`);
        remoteTools.forEach((tool, index) => {
            console.log(`${index + 1}. 🌐 ${tool.name} - ${tool.description.substring(0, 40)}...`);
        });
        console.log('');
        
        console.log('⚡ 步骤5: 测试直接调用远程工具');
        if (remoteTools.length > 0) {
            const firstRemoteTool = remoteTools[0];
            console.log(`尝试调用: ${firstRemoteTool.name}`);
            
            const remoteCallResult = await toolManager.executeTool(firstRemoteTool.name, {
                dir: '.',
                maxDepth: 1
            });
            
            console.log('远程工具调用结果:');
            console.log(remoteCallResult.substring(0, 200) + '...');
        }
        console.log('');
        
        console.log('❌ 步骤6: 断开连接');
        await toolManager.executeTool('mcp-client', {
            action: 'disconnect',
            serverName: serverName
        });
        
        console.log('🎉 测试完成！MCP工具自动注册功能正常工作！');
        
    } catch (error) {
        console.error('❌ 测试失败:', error.message);
    }
}

testMCPAutoRegister();